<?php
namespace app\api\controller\shui;

use app\common\controller\Api;
use app\common\model\User;
use think\Db;
use think\Exception;

class Rechange extends Api
{
    protected $noNeedRight = ['*'];
    protected $noNeedLogin = ['machine'];


    // 设备信息
    public function machine()
    {
        $sn = $this->request->param('sn');

        $result = Db::name('zhinenggui_counter_list')->where('sn_code',$sn)->find();
        if ($result!==false){
            $this->success('成功',$result);
        }else{
            $this->error('失败');
        }
    }

    public function open()
    {
        $sn = $this->request->param('sn');
        $type = $this->request->param('type');

        $user = User::get($this->auth->id);


        $machine = Db::name('zhinenggui_counter_list')->where('sn_code',$sn)->find();
        if (!$machine){
            $this->error('设备不存在');
        }

        try
        {
            if ($type){
                // 余额
                // 判断金额
                if ($user['money']<1.5){
                    \exception('金额不足');
                    return false;
                }

                User::money(-1.5,$this->auth->id,'余额打水');
            }else{
                if ($user['score']< 30){
                    \exception('积分不足');
                    return false;
                }
                // 积分
                User::score(-30,$this->auth->id,'积分打水');
            }
        }catch (Exception $exception){
            $this->error($exception->getMessage());
        }

        $this->success('放水成功');

    }
}